package com.turman.common.net.model

import java.lang.Exception

/**
 * 数据响应封装类1
 */
sealed class DataResult<out R> {
    //成功
    data class Success<out T>(val data:T): DataResult<T>()
    //失败
    data class Error(val exception: Exception): DataResult<Nothing>()
    //加载数据中
    object Loading: DataResult<Nothing>()

    override fun toString(): String {
        return when(this) {
            is Success<*> ->"Success[data=$data]"
            is Error ->"Error[exception=$exception]"
            Loading ->"Loading"
        }
    }
}

//扩展字段，为success并且数据不为空才认为是成功
val DataResult<*>.successed:Boolean
    get() = this is DataResult.Success && data!=null


/**
 * 数据响应封装2
 */
enum class Status {
    SUCCESS,
    ERROR,
    LOADING
}

data class Resource<out T>(val status: Status, val data:T?, val message:String?) {
    companion object {
        fun <T> success(data:T?): Resource<T> {
            return Resource(Status.SUCCESS,data,"Resource Success")
        }

        fun <T> error(msg:String, data:T?): Resource<T> {
            return Resource(Status.ERROR,data,msg)
        }

        fun <T> loading(data:T?): Resource<T> {
            return Resource(Status.LOADING,data,null)
        }
    }
}

















